//
//  PopoverDemo.swift
//  SwiftUIDemo
//
//  Created by Tilt Wang on 2025/6/29.
//

import SwiftUI

struct PopoverDemo: View {
    
    @State var showNewScreen: Bool = false
    
    var body: some View {
        ZStack {
            Color.orange
                .edgesIgnoringSafeArea(.all)
            
            VStack {
                Button {
                    showNewScreen.toggle()
                } label: {
                    Text("Button")
                        .foregroundColor(.green)
                        .font(.headline)
                        .padding(20)
                        .background(Color.white.cornerRadius(10))
                }
                Spacer()
                // 1. sheet
//                .sheet(isPresented: $showNewScreen) {
//                    NewScreen()
//                }
                // 2. fullScreen sheet
//                .fullScreenCover(isPresented: $showNewScreen) {
//                    NewScreen()
                // 3. transtion
//                ZStack {
//                    if showNewScreen {
//                        NewScreen(showNewScreen: $showNewScreen)
//                            .padding(.top, 100)
//                            .transition(.move(edge: .bottom))
//                            .animation(.spring())
//                    }
//                }
//                .zIndex(2.0)
                //4. offset
                NewScreen(showNewScreen: $showNewScreen)
                    .padding(.top, 100)
                    .offset(y: showNewScreen ? 0 : UIScreen.main.bounds.height)
                    .animation(.spring())             
                
            }
            
        }
        
    }
}

struct NewScreen: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    @Binding var showNewScreen: Bool
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            Color.purple
                .edgesIgnoringSafeArea(.all)
            
            Button {
                showNewScreen.toggle()
//                presentationMode.wrappedValue.dismiss()
            } label: {
                //                Text("Button")
                Image(systemName: "xmark")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .padding(20)
                //                    .background(Color.white.cornerRadius(10))
            }
            //            .sheet(isPresented: $showSheet) {
            //                Text("Hello there")
            //            }
            
        }
    }
    
}

#Preview {
    PopoverDemo()
}
